package cn.servlet;

import util.DatabaseType;
import util.JdbcHelper;
import util.RowMapper;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/userlist")
public class UserListServlet extends HttpServlet {
    private List<User> list=new ArrayList<>();
    private User u=null;
    protected void service(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw=response.getWriter();
        JdbcHelper jh=JdbcHelper.getInstance(DatabaseType.MYSQL);
        String sql="select id,name,gender,birthdate from t_users";
        RowMapper<User> urow=new RowMapper<User>() {
            @Override
            public User wrap(ResultSet rs) throws SQLException {
                u=new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setGender(rs.getString(3));
                u.setBirthdate(rs.getDate(4));
                return u;
            }
        };
        list=jh.query(sql,urow);
        Iterator<User> it= list.iterator();
        pw.print("<table style='border:1px solid red; border-collapse:collapse;margin:auto'>");
        pw.print("<tr style='text-align:center;'><td height='50px' colspan='8' style='border:1px solid red;line-height:50px;font-size:20px; border-collapse:collapse;color:red'>所有人员信息如下：</td></tr>");
        while (it.hasNext()){
            User us=it.next();
            pw.print("<tr><td width='100px' style='border:1px solid red; border-collapse:collapse; text-align: right'>学号：</td><td width='100px' style='border:1px solid red; border-collapse:collapse;'>"+us.getId()+" </td>");
            pw.print("<td width='100px' style='border:1px solid red; border-collapse:collapse;text-align: right'>性别:</td><td width='100px' style='border:1px solid red; border-collapse:collapse;'>"+us.getGender()+"</td>");
            pw.print("<td width='100px' style='border:1px solid red; border-collapse:collapse;text-align: right'>姓名:</td><td width='100px' style='border:1px solid red; border-collapse:collapse;'>"+us.getName()+"</td>");
            pw.println("<td width='100px' style='border:1px solid red; border-collapse:collapse;text-align: right'>出生日期:</td><td width='100px' style='border:1px solid red; border-collapse:collapse;'>"+us.getBirthdate()+"</td></tr>");
        }
        pw.print("</table>");
        pw.print("<div style='margin:20px auto;text-align:center;color:blue;font-size:20px'><a href='/index.html'>回到主页</a></div>");
    }
}
